/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.services.impl;

import cz.muni.fi.pa165.creatures.dao.CreatureDAO;
import cz.muni.fi.pa165.creatures.dto.CreatureDTO;
import cz.muni.fi.pa165.creatures.dto.CreatureTypeDTO;
import cz.muni.fi.pa165.creatures.dto.RegionDTO;
import cz.muni.fi.pa165.creatures.dto.mapping.CreatureMapping;
import cz.muni.fi.pa165.creatures.dto.mapping.CreatureTypeMapping;
import cz.muni.fi.pa165.creatures.dto.mapping.RegionMapping;
import cz.muni.fi.pa165.creatures.model.Creature;
import cz.muni.fi.pa165.creatures.model.CreatureType;
import cz.muni.fi.pa165.creatures.model.Region;
import cz.muni.fi.pa165.creatures.services.CreatureService;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * This class represents actual implementation of the
 * {@link cz.muni.fi.pa165.creatures.services.CreatureService}.
 *
 * @author xmiklos1
 */
@Service
public final class CreatureServiceImpl implements CreatureService {

    private static final Logger logger =
            Logger.getLogger(CreatureServiceImpl.class.getName());

    @Autowired
    private CreatureDAO creatureDAO;

    @Autowired
    private CreatureMapping creatureMapping;

    @Autowired
    private RegionMapping regionMapping;

    @Autowired
    private CreatureTypeMapping creatureTypeMapping;

    @Override
    @Transactional
    public Long create(final CreatureDTO creature) {
        try {
            return creatureDAO.create(creatureMapping.DTOtoEntity(creature));
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional
    public void remove(final CreatureDTO creature) {
        try {
            creatureDAO.remove(creatureMapping.DTOtoEntity(creature));
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional
    public void update(final CreatureDTO creature) {
        try {
            creatureDAO.update(creatureMapping.DTOtoEntity(creature));
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional(readOnly = true)
    public CreatureDTO getById(final Long id) {
        try {
            return creatureMapping.entityToDTO(creatureDAO.get(id));
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional(readOnly = true)
    public CreatureDTO getByName(final String name) {
        try {
            return creatureMapping.entityToDTO(creatureDAO.getByName(name));
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional(readOnly = true)
    public List<CreatureDTO> getAll() {
        try {
            List<CreatureDTO> dtos = new ArrayList<CreatureDTO>();
            for (Creature c : creatureDAO.getAll()) {
                dtos.add(creatureMapping.entityToDTO(c));
            }
            return dtos;
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional(readOnly = true)
    public List<CreatureDTO> getCreaturesOf(final RegionDTO region) {
        try {
            Region regionEntity = regionMapping.DTOtoEntity(region);
            List<Creature> creatures = creatureDAO.getCreaturesOf(regionEntity);
            List<CreatureDTO> creaturesDTO = new ArrayList<CreatureDTO>();
            for (Creature creature : creatures) {
                creaturesDTO.add(creatureMapping.entityToDTO(creature));
            }
            return creaturesDTO;
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional(readOnly = true)
    public List<CreatureDTO> getCreaturesOf(final CreatureTypeDTO creatureType) {
        try {
            CreatureType creatureTypeEntity =
                    creatureTypeMapping.DTOtoEntity(creatureType);
            List<Creature> creatures =
                    creatureDAO.getCreaturesOf(creatureTypeEntity);
            List<CreatureDTO> creaturesDTO = new ArrayList<CreatureDTO>();
            for (Creature creature : creatures) {
                creaturesDTO.add(creatureMapping.entityToDTO(creature));
            }
            return creaturesDTO;
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional(readOnly = true)
    public Long getCount() {
        try {
            return creatureDAO.getCount();
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional(readOnly = true)
    public Long getCountOfCreaturesOf(final RegionDTO region) {
        try {
            return Long.valueOf(this.getCreaturesOf(region).size());
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional(readOnly = true)
    public Long getCountOfCreaturesOf(final CreatureTypeDTO creatureType) {
        try {
            return Long.valueOf(this.getCreaturesOf(creatureType).size());
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    public void deleteAll() {
        try {
            creatureDAO.deleteAll();
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }
}